package com.deyuanyun.pic.pbf.controller.dto.validata;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.common.util.web.RequestResponseContext;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;

/**
 * 验证桩或者场站阀室是否存在
 * 
 * @author ChenKui
 *
 */
public class PipeToPointRequestValidata implements ValidateInterface {

	@Override
	public <T, E,C> boolean apply(T t, E e,C c) {
		
		if(ObjectUtil.isNotNullEmpty(t)){
			try {
				String pointName = BeanUtils.getProperty(c,"toPointName");
				/*
				String segmentName = (String)t;
				String segmentId = PipeDictUtils.getSegmentByName(lineName, segmentName);
				 */
				
				if (StringUtils.isEmpty(pointName)){
				    return false;
				}

                HttpServletRequest request = RequestResponseContext.getRequest();
                String pointId_request = request.getParameter("toPointId");
                String lineId= PipeDictUtils.getLineIdByName(((String) t).trim());
                PipeLocationService pipeLocationService = SpringAppliction.getBean(PipeLocationService.class);
                PipeLocationDTO pipeLocation = pipeLocationService.queryPipeLocationById(lineId,pointId_request);
                Asserts.assertTrue(pipeLocation != null, "没有得到终点地址、" + pointId_request);
                
                String pointName_request = pipeLocation.getName();
                return pointName.trim().equals(pointName_request);

			} catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}

		}
		return true;
	}

	@Override
	public String getName() {
		
		return null;
	}

}
